<?php
/**********************
**register.php
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

/**
 * This is the page that will be displayed after the
 * registration has been attempted.
 */
require_once("private/bottom.inc");
require_once("private/user.inc");
require_once("private/top.inc");
require_once("private/shared/form.inc");

function displayPage()
{
	/**
	 * This is the page with the sign-up form, the names
	 * of the input fields are important and should not
	 * be changed.
	 */
	top("Register");

	if( TT_FAILED(checkRegisterUser()) )
	{
		echo "Error checking user registration<BR />";
	}
	
	echo "
		<FORM name = \"RegisterForm\" action=\"register.php\" method=\"post\">
		<table align=\"left\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">
		<TR>
			<TD>
				Real Name:
			</TD>
			<TD>
		";
				echo "<input type=\"text\" name=\"" . FORM_USER_NAME . "\" maxlength=\"24\">";
	echo "
			</TD>
		</TR>
		<TR>
			<TD>
				Username:
			</TD>
			<TD>
		";
				echo "<input type=\"text\" name=\"" . FORM_USER_USERNAME . "\" maxlength=\"24\">";
	echo "
			</TD>
		</TR>
		<TR>
			<TD>
				Password:
			</TD>
			<TD>
		";
				echo "<input type=\"password\" name=\"" . FORM_USER_PASSWORD . "\" maxlength=\"24\">";
	echo "
			</TD>
		</TR>
		<TR>
			<TD>
		";
				//Access groups are currently not supported, so hide the items and make default access for now		
				//echo "Access Group:";
	echo "
			</TD>
			<TD>
		";
				//echo "<input type=\"text\" name=\"" . FORM_USER_GROUP . "\" maxlength=\"24\">";
				echo "<input type=\"hidden\" name=\"" . FORM_USER_GROUP . "\" value=1 maxlength=\"24\">";
	echo "
			</TD>
		</TR>
		<TR>
			<TD colspan=\"2\" align=\"right\">
				<input type=\"submit\" name=\"submit\" value=\"&nbsp;&nbsp;Join!&nbsp;&nbsp;\">
			</TD>
		</TR>
		";
	/*
	TODO: discuss reactivating this
	Is this redundant to the top GUI?
	echo "
		<TR>
			<TD colspan=\"2\" align=\"right\">
				<FORM>	
					<input type=\"submit\" name=\"cancel\" value=\"Cancel\">
				</FORM>
			</TD>
		</TR>
		";
	*/
	
	echo "
		</TABLE>
		</FORM>
		<SCRIPT type=\"text/javascript\" language=\"JavaScript\">
		document.forms['RegisterForm'].elements['" . FORM_USER_NAME . "'].focus();
		</SCRIPT>
		<BR /><BR /><BR /><BR /><BR /><BR /><P>
		";

	bottom();
}

//Functions that have to do with registering a User.
 /**
 * Displays the appropriate message to the user
 * after the registration attempt.
 */
function checkRegisterUser()
{
	/**
	 * Determines whether or not to show the sign-up form
	 * based on whether the form has been submitted, if it
	 * has, check the database for consistency and create
	 * the new account.
	 */

	//Make sure all fields were entered to even try to process the request
	if( !isset($_POST[FORM_USER_USERNAME]) || !isset($_POST[FORM_USER_PASSWORD]) ||!isset($_POST[FORM_USER_NAME]) || !isset($_POST[FORM_USER_GROUP]) )
	{
		return TT_OK;
	}
	
	$username = trim($_POST[FORM_USER_USERNAME]);
	if( $username == '' )
	{
		echo 'Username required.<BR />';
		return TT_OK;
	}
	if( strlen($username) > USERNAME_MAX_LENGTH )
	{
		echo "Sorry, the username is longer than 24 characters, please shorten it.<BR />";
		return TT_OK;
	}
	//Check if username is already in use
	$rc = User::usernameTaken($username, $isTaken);
	if( TT_FAILED($rc) )
	{
		return $rc;
	}
	if( $isTaken )
	{
	 	echo "Sorry, the username: <B>" . $username . "</B> is already taken, please pick another one.<BR />";
		return TT_OK;
	}

	$password = $_POST[FORM_USER_PASSWORD];
	if( $password == '' )
	{
		echo 'Password required.<BR />';
		return TT_OK;
	}

	$name = $_POST[FORM_USER_NAME]; 
	if( !$name )
	{
		echo 'Name required.<BR />';
		return TT_OK;
	}

	$accessGroup = $_POST[FORM_USER_GROUP];
	if( !$accessGroup )
	{
		echo 'Group required.<BR />';
		return TT_OK;
	}

	$fields = array('username' => $username, 'password' => $password, 'name' => $name, 'accessGroup' => $accessGroup);
	$rc = User::createNewUser($user, $fields);
	if( TT_SUCCEEDED($rc) )
	{
		echo"
			<H1>Registered!</H1>
			<B>" . $username ."</B> has been added to the database.
			";
	}
	else
	{
		echo"
			<H1>Registration Failed</H1>
			We're sorry, but an error has occurred and your registration for the username <b>". $username ."</b>, could not be completed.<BR />
			Please try again at a later time.<BR />
			";
		echo "Server error: " . User::getLastError() . "<BR />";
	}
	echo "
		<BR />
		<BR />
		";
		
	return TT_OK;
}

displayPage();
?>
